package com.ky.home.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.vo.R;
import com.ky.home.entity.WordBook;
import com.ky.home.mapper.WordBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/home")
public class WordBookController {
    @Autowired
    WordBookMapper wordBookMapper;
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/wordBook/getWordBook")
    public R getWordBook() {
        // 从redis中取出单词本id
        if (redisTemplate.opsForValue().get("wordBookId") == null) {
            // 存储数据到redis
            redisTemplate.opsForValue().set("wordBookId", 1 + "");
            QueryWrapper<WordBook> wordBookQueryWrapper = new QueryWrapper<>();
            wordBookQueryWrapper.eq("id", Integer.parseInt(redisTemplate.opsForValue().get("wordBookId")));

            return R.ok(wordBookMapper.selectOne(wordBookQueryWrapper));
        }
        // 得到单词本id
        QueryWrapper<WordBook> wordBookQueryWrapper = new QueryWrapper<>();
        wordBookQueryWrapper.eq("id", Integer.parseInt(redisTemplate.opsForValue().get("wordBookId")));
        return R.ok(wordBookMapper.selectOne(wordBookQueryWrapper));
    }
    @RequestMapping("/wordBook/updateWordBook/{id}")
    public R updateWordBook(@PathVariable int id) {
        redisTemplate.opsForValue().set("wordBookId", id + "");

        return R.ok();
    }
    @RequestMapping("/wordBook/selectAllWordBook")
    public R selectAllWordBook(){
        return R.ok(wordBookMapper.selectList(null));
    }
}

